<template>
  <div class="page-container">
    <div class="hero-section">
      <div class="section-header">
        <h1>校园活动</h1>
        <p class="subtitle">发现精彩，参与互动</p>
      </div>
    </div>

    <div class="main-content">
      <div class="filter-search-container">
        <div class="filter-section">
          <div class="section-title">
            <span class="dot"></span>
            <h3>活动分类</h3>
          </div>
          <el-radio-group v-model="radio" size="large">
            <el-radio-button
              label="全部"
              value="全部"
              @change="searchByCategoryId(null)"
            />
            <div v-for="(category, index) in categorys" :key="index">
              <el-radio-button
                :label="category.categoryName"
                :value="category.id"
                @click="searchByCategoryId(category.id)"
              />
            </div>
          </el-radio-group>
        </div>

        <div class="search-section">
          <div class="section-title">
            <span class="dot"></span>
            <h3>搜索活动</h3>
          </div>
          <el-input
            v-model="search.title"
            placeholder="搜索你感兴趣的活动..."
            clearable
            :prefix-icon="Search"
            @change="onSearchChange"
          >
            <template #append>
              <el-button :icon="Search" @click="onSearchChange" />
            </template>
          </el-input>
        </div>
      </div>

      <div class="content-section">
        <div class="section-title content-title">
          <span class="dot"></span>
          <h2>活动列表</h2>
          <p class="section-description">
            {{ pagination.total }}个活动等你参与
          </p>
        </div>

        <div v-if="activities.length === 0" clases="no-results">
          <el-empty description="暂无相关活动" />
        </div>

        <div v-else class="activity-grid">
          <ActivityItem
            v-for="(activity, index) in activities"
            :key="index"
            :activity="activity"
            @click="navigateToActivity(activity.id)"
            class="activity-item-animation"
          />
        </div>

        <div v-if="pagination.total > 0" class="pagination-wrapper">
          <el-pagination
            :page-sizes="[5, 10, 20, 50]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pagination.total"
            :current-page="pagination.page"
            :page-size="pagination.size"
            @size-change="onPageSizeChange"
            @current-change="onCurrentPageChange"
          />
        </div>
      </div>

      <!-- Recommendations Section -->
      <div class="favorite-section">
        <div class="section-title">
          <span class="dot"></span>
          <h2>个性化推荐</h2>
          <p class="section-description">为你推荐感兴趣的活动</p>
        </div>
        <div class="activity-grid recommendations">
          <ActivityItem
            v-for="(activity, index) in favoriteProducts"
            :key="index"
            :activity="activity"
            @click="navigateToActivity(activity.id)"
            class="activity-item-animation"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import ActivityItem from "@/components/ActivityItem.vue";
import { getAllCategory } from "@/apis/admin/category";
import { activityPage, baseUserRecommend } from "@/apis/activity";
import { reactive, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { Search } from "@element-plus/icons-vue";

// 路由
const router = useRouter();
// 数据
const activities = ref([]);

// 实现跳转活动详情页,传递路径参数
const navigateToActivity = (activityId) => {
  router.push({
    name: "activityDetail",
    params: { activityId: activityId },
  });
};

// 分页
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0,
});

// 分页改变函数
const onCurrentPageChange = (currentPage) => {
  pagination.page = currentPage;
  loadActivities();
};

const onPageSizeChange = (pageSize) => {
  pagination.size = pageSize;
  loadActivities();
};

// 搜索输入框搜索条件
const search = reactive({
  title: "",
  categoryId: "",
});

// 过滤对应的条件
const filter = ref({});

// 存储排序字段
const orders = reactive({
  column: "id",
  isAsc: true,
});

// 数据
const favoriteProducts = ref([]);

// 个性化推荐模块
const getFavoritePage = () => {
  baseUserRecommend().then((result) => {
    favoriteProducts.value = result.data;
  });
};

// 加载活动列表
const loadActivities = () => {
  orders.column = "create_time"; // 按创建时间排序
  orders.isAsc = false; // 最新的优先

  activityPage(
    {
      filter: filter.value,
      search: search,
      orders: [orders],
    },
    pagination.page,
    pagination.size
  ).then((result) => {
    activities.value = result.data.records;
    pagination.total = result.data.total;
  });
};

const radio = ref("全部");
const categorys = ref([]);

// 页面加载时执行
onMounted(async () => {
  try {
    // 加载分类数据
    const categoryResult = await getAllCategory();
    categorys.value = categoryResult.data;

    // 加载活动数据
    loadActivities();

    // 加载推荐数据
    getFavoritePage();
  } catch (error) {
    console.error("数据加载失败", error);
  }
});

// 点击后弹出数据
const searchByCategoryId = (categoryId) => {
  search.categoryId = categoryId;
  pagination.page = 1; // 重置到第一页
  loadActivities();
};

// 选择框发生改变
const onSearchChange = () => {
  pagination.page = 1; // 重置到第一页
  loadActivities();
};
</script>

<style scoped>
/* Base Styles */
.page-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px 60px;
  color: #1d1d1f;
}

/* Hero Section */
.hero-section {
  background: linear-gradient(135deg, #0066cc 0%, #5cacff 100%);
  border-radius: 0 0 30px 30px;
  padding: 80px 20px;
  margin: 0 -20px 40px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.hero-section::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: url("data:image/svg+xml,%3Csvg width='100' height='100' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M11 18c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm48 25c3.866 0 7-3.134 7-7s-3.134-7-7-7-7 3.134-7 7 3.134 7 7 7zm-43-7c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm63 31c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM34 90c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zm56-76c1.657 0 3-1.343 3-3s-1.343-3-3-3-3 1.343-3 3 1.343 3 3 3zM12 86c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm28-65c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm23-11c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-6 60c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm29 22c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zM32 63c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm57-13c2.76 0 5-2.24 5-5s-2.24-5-5-5-5 2.24-5 5 2.24 5 5 5zm-9-21c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM60 91c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM35 41c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2zM12 60c1.105 0 2-.895 2-2s-.895-2-2-2-2 .895-2 2 .895 2 2 2z' fill='rgba(255,255,255,0.1)' fill-rule='evenodd'/%3E%3C/svg%3E");
  opacity: 0.5;
}

.section-header h1 {
  font-size: 52px;
  font-weight: 700;
  color: #ffffff;
  margin-bottom: 16px;
  letter-spacing: -0.5px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.subtitle {
  font-size: 24px;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 400;
}

/* Main Content */
.main-content {
  position: relative;
  z-index: 1;
}

/* Filter & Search Section */
.filter-search-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  margin-bottom: 40px;
}

.filter-section,
.search-section {
  background: #ffffff;
  border-radius: 20px;
  padding: 32px;
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.filter-section:hover,
.search-section:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.08);
}

/* Section Titles */
.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
  gap: 12px;
}

.content-title {
  margin-bottom: 32px;
}

.section-title h2,
.section-title h3 {
  font-size: 28px;
  font-weight: 600;
  color: #1d1d1f;
  margin: 0;
}

.dot {
  width: 10px;
  height: 10px;
  background: #0066cc;
  border-radius: 50%;
  box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.2);
}

.section-description {
  font-size: 17px;
  color: #86868b;
  margin-top: 8px;
  margin-left: 20px;
}

/* Activity Grid */
.content-section {
  margin-bottom: 60px;
}

.activity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.activity-item-animation {
  transition: all 0.3s cubic-bezier(0.25, 1, 0.5, 1);
}

.activity-item-animation:hover {
  transform: translateY(-6px);
  box-shadow:
    0 14px 28px rgba(0, 0, 0, 0.12),
    0 10px 10px rgba(0, 0, 0, 0.08);
}

/* Pagination */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin: 40px 0;
  padding: 24px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.04);
}

/* Favorites Section */
.favorite-section {
  margin-top: 80px;
  padding: 60px 40px;
  background: linear-gradient(180deg, #f5f5f7 0%, #ffffff 100%);
  border-radius: 30px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
}

.favorite-section::after {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  width: 300px;
  height: 300px;
  background: radial-gradient(
    circle,
    rgba(0, 102, 204, 0.08) 0%,
    rgba(0, 102, 204, 0) 70%
  );
  z-index: -1;
}

.recommendations {
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
}

/* No Results */
.no-results {
  text-align: center;
  padding: 80px 0;
  background: #f9f9fb;
  border-radius: 20px;
  margin-bottom: 40px;
}

/* Element Plus Overrides */
:deep(.el-radio-group) {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

:deep(.el-radio-button__inner) {
  border-radius: 12px !important;
  padding: 12px 24px;
  font-size: 15px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: none;
  background: #f5f5f7;
  color: #1d1d1f;
}

:deep(.el-radio-button__original-radio:checked + .el-radio-button__inner) {
  background: #0066cc;
  color: white;
  box-shadow: 0 6px 16px rgba(0, 102, 204, 0.25);
}

:deep(.el-input) {
  width: 100% !important;
}

:deep(.el-input .el-input__wrapper) {
  border-radius: 14px;
  padding: 12px 16px;
  background: #f5f5f7;
  border: none;
  box-shadow: none !important;
  transition: all 0.3s ease;
}

:deep(.el-input .el-input__wrapper:hover),
:deep(.el-input .el-input__wrapper.is-focus) {
  background: #ebebeb;
}

:deep(.el-input-group__append) {
  border-top-right-radius: 14px !important;
  border-bottom-right-radius: 14px !important;
  background-color: #0066cc !important;
  border-color: #0066cc !important;
}

:deep(.el-input-group__append .el-button) {
  color: white !important;
  border: none !important;
}

:deep(.el-pagination) {
  --el-pagination-hover-color: #0066cc;
  --el-pagination-button-color: #0066cc;
  font-weight: 500;
}

:deep(.el-pagination .el-pagination__sizes .el-input .el-input__wrapper) {
  border-radius: 10px;
}

:deep(.el-pagination .btn-prev),
:deep(.el-pagination .btn-next) {
  border-radius: 10px;
  transition: all 0.3s ease;
}

:deep(.el-pagination .el-pager li) {
  border-radius: 10px;
  transition: all 0.3s ease;
}

:deep(.el-pagination .el-pager li.is-active) {
  background-color: #0066cc;
  font-weight: 600;
}

:deep(.el-empty) {
  padding: 40px;
}

/* Responsive Design */
@media (max-width: 992px) {
  .filter-search-container {
    grid-template-columns: 1fr;
  }

  .activity-grid {
    grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  }
}

@media (max-width: 768px) {
  .section-header h1 {
    font-size: 42px;
  }

  .subtitle {
    font-size: 20px;
  }

  .hero-section {
    padding: 60px 20px;
  }

  .activity-grid {
    grid-template-columns: 1fr;
  }

  .filter-section,
  .search-section {
    padding: 24px;
  }

  .favorite-section {
    padding: 40px 24px;
  }
}

@media (max-width: 480px) {
  .section-header h1 {
    font-size: 36px;
  }

  .subtitle {
    font-size: 18px;
  }

  .section-title h2,
  .section-title h3 {
    font-size: 22px;
  }
}
</style>
